﻿using EarringsBackEnd.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EarringsBackEnd
{
    public partial class frmEarringsModel : Form
    {
        private string[] _strArrHeader = null;
        private string _groupName = string.Empty;

        public frmEarringsModel()
        {
            InitializeComponent();
        }

        public frmEarringsModel(string groupName)
        {
            InitializeComponent();
            _groupName = groupName;

            this.LoadErGroup();
        }

        private void frmEarringsModel_Load(object sender, EventArgs e)
        {
            txtErGroupName.Text = _groupName;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string sqlStr = string.Empty;
            sqlStr = "INSERT INTO EarringsModel (ModelName, Er_Code) VALUES ('" + txtErGroupName.Text + "','" + txtErCode.Text + "')";

            SqlClientUtility.ExecuteNonQuery(CommandType.Text, sqlStr);

            this.LoadErGroup();

            //int no = 0;
            //int n = dtgvGrpEarrings.Rows.Add();            
            //dtgvGrpEarrings.Rows[n].Cells[0].Value = n;

            //string erName = CheckHasErCodeInDB();

            //if (!string.IsNullOrEmpty(erName))
            //{
            //    no = n + 1;
            //    dtgvGrpEarrings.Rows[n].Cells[0].Value = no;
            //    dtgvGrpEarrings.Rows[n].Cells[1].Value = txtErCode.Text;
            //    dtgvGrpEarrings.Rows[n].Cells[2].Value = erName;
            //}
            //else
            //{
            //    MessageBox.Show("ไม่พบข้อมูลรหัสต่างหู");
            //}
        }

        private string  CheckHasErCodeInDB ()
        {
            string strSql = string.Empty;

            strSql = "SELECT Er_Name FROM Earrings WHERE Er_Code = '" + txtErCode.Text + "'";

            string name = string.Empty;
            name = SqlClientUtility.ExecuteScalar(CommandType.Text, strSql).ToString();

            return name;
        }

        private void dtgvGrpEarrings_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowIndex = e.RowIndex;
            DataGridViewRow row = dtgvGrpEarrings.Rows[rowIndex];

            int colIndex = e.ColumnIndex;
            DataGridViewColumn col = dtgvGrpEarrings.Columns[colIndex];

             if (col.HeaderText.ToString() == "ลบ" && !string.IsNullOrEmpty(row.Cells[1].Value.ToString()))
             {
                 dtgvGrpEarrings.Rows.RemoveAt(rowIndex);

                 //Delete From DB
                 string sqlStr = string.Empty;
                 sqlStr = "DELETE FROM EarringsModel WHERE ModelName = '" + txtErGroupName.Text + "' AND Er_Code = '" + txtErCode.Text + "'";
                 SqlClientUtility.ExecuteNonQuery(CommandType.Text, sqlStr);
             }

            // Re No.
             int rows = dtgvGrpEarrings.Rows.Count-1;
             for (int i = 0; i < rows; i++ )
             {
                 DataGridViewRow dgvr = dtgvGrpEarrings.Rows[i];
                 dgvr.Cells[0].Value = i+1;
             }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearValue();
        }

        private void ClearValue()
        {
            foreach (Control ctl in tblLayout.Controls)
            {
                TextBox tb = ctl as TextBox;

                if (tb != null)
                {
                    tb.Clear();
                }
            }
            cboStyle.SelectedIndex = 0;
            cboShape.SelectedIndex = 0;

            dtgvGrpEarrings.Rows.Clear();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.ClearValue();

            this.Dispose();
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string strSql = string.Empty;
            string modelName = txtErGroupName.Text;

            foreach (DataGridViewRow row in dtgvGrpEarrings.Rows)
            {
                string erCode = row.Cells[1].Value.ToString();
                strSql = "INSERT INTO EarringsModel (ModelName, Er_Code) VALUES ('" + modelName + "','" + erCode + "')";

                SqlClientUtility.ExecuteNonQuery(CommandType.Text, strSql);
            }
        }

        private void btnViewErCode_Click(object sender, EventArgs e)
        {

        }

        private void LoadErGroup()
        {
            StringBuilder sqlStr = new StringBuilder();

            sqlStr.Append("SELECT erm.Er_Code,erm.Er_Name,erm.Er_Image_Front, ");        
            sqlStr.Append(" (SELECT Config_Name FROM Earrings er ");
            sqlStr.Append(" INNER JOIN EarringsConfig conf ON er.ER_Style = conf.Config_Id ");
            sqlStr.Append(" WHERE conf.Short_Type ='ERSTY' AND er.Er_Code = erm.Er_Code) AS 'Style'");
            sqlStr.Append(" FROM EarringsModel mo ");
            sqlStr.Append(" INNER JOIN Earrings erm ON mo.Er_Code = erm.Er_Code");
            sqlStr.Append(" WHERE mo.ModelName ='").Append(_groupName).Append("'");

            DataTable dtResult = new DataTable();
            dtResult = SqlClientUtility.ExecuteDataTable(CommandType.Text, sqlStr.ToString());

            string style = string.Empty;
            string savePath = Resources.EarringsPath;
            string folderEarrings = string.Empty;
            int n = 0;
            Bitmap bmpImage = null;

            foreach (DataRow row in dtResult.Rows)
            {
                style = row["Style"].ToString();
                folderEarrings = Path.Combine(savePath, style);
                string filePath = Path.Combine(folderEarrings, row["Er_Image_Front"].ToString());
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                bmpImage = (Bitmap)Image.FromStream(fs);
                Image NewImage = bmpImage.GetThumbnailImage(100, 100, null, IntPtr.Zero);

                n = dtgvGrpEarrings.Rows.Add();
                dtgvGrpEarrings.Rows[n].Height = 100;
                dtgvGrpEarrings.Rows[n].Cells[0].Value = n + 1;
                dtgvGrpEarrings.Rows[n].Cells[1].Value = row["Er_Code"].ToString();
                dtgvGrpEarrings.Rows[n].Cells[2].Value = NewImage;
                dtgvGrpEarrings.Rows[n].Cells[3].Value = row["Er_Name"].ToString();
            }
        }

        private void tableLayoutPanel2_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
